package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.web.controller.BasicController;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

/**
 * 
 * 云供应商Controller
 * 
 * @author DZG
 * @since V1.0 2016年9月1日
 */
@RestController
@RequestMapping("/cloudVendor")
public class CloudVendorController {

	private final String BASE_SERVICE = "/cloudVendor";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;
	@Autowired
	private BasicController basicController;

	/**
	 * 创建云供应商
	 * 
	 * @param params
	 *            云供应商对象
	 * @return
	 */
	@RequestMapping(value = "/create", method = { RequestMethod.POST })
	public BsmResult create(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		return basicController.create(params, BASE_SERVICE, request, CloudVendorController.class.getSimpleName());
	}

	/**
	 * 云供应商修改
	 * 
	 * @return
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST })
	public BsmResult modify(@RequestParam(value = Common.PARAMS, required = false) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONTools.isJSONObj(params);
		if (null != jsonObject) {
			String url = BASE_SERVICE + "/modify";
			Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.CREATE, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 移除云供应商
	 * 
	 * @return
	 */
	@RequestMapping(value = "/remove", method = { RequestMethod.POST })
	public BsmResult remove(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.remove(params, BASE_SERVICE, request, CloudVendorController.class.getSimpleName());
	}

	/**
	 * 获取云供应商列表
	 * 
	 * @param page
	 *            当前页码
	 * @param rows
	 *            页面数据大小
	 * @param params
	 *            查询参数
	 * @param sorter
	 *            排序参数，例如：{"name":0|1,"password":0|1},0表示增序，1表示降序
	 * @param simple
	 *            简单查询标记，只有true和false,为false时返回资源池的详细信息，为true时只返回id和name值
	 * @param request
	 *            请求对象
	 * @return
	 */
	@RequestMapping(value = "/list", method = { RequestMethod.POST })
	public BsmResult list(@RequestParam(value = Common.PAGE, required = false, defaultValue = Common.ONE) Integer page,
			@RequestParam(value = Common.ROWS, required = false, defaultValue = Common.TEN) Integer rows,
			@RequestParam(value = Common.PARAMS, required = false) String params,
			@RequestParam(value = Common.SORTER, required = false) String sorter,
			@RequestParam(value = Common.SIMPLE, required = false) boolean simple, HttpServletRequest request) {
		return basicController.list(page, rows, params, sorter, simple, BASE_SERVICE, request,
				CloudVendorController.class.getSimpleName());
	}

	/**
	 * 冻结云供应商
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/lock", method = { RequestMethod.POST })
	public BsmResult lock(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject obj = JSONObject.parseObject(params);
		String url = UrlTranslator.translate(BASE_SERVICE + "/lock", obj.getLong(Common.ID));
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OTHERS, null, null, request);
		return service.invoke();
	}

	/**
	 * 解冻云供应商
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/active", method = { RequestMethod.POST })
	public BsmResult active(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject obj = JSONObject.parseObject(params);
		String url = UrlTranslator.translate(BASE_SERVICE + "/active", obj.getLong(Common.ID));
		RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OTHERS, null, null, request);
		return service.invoke();
	}

	/**
	 * 云供应商详细
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	public BsmResult detail(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		return basicController.detail(params, BASE_SERVICE, request, CloudVendorController.class.getSimpleName());
	}
	
	
	/**
     * 同步
     * 
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value = "/sync", method = { RequestMethod.POST })
    public BsmResult sync(@RequestParam(value = Common.PARAMS, required = false) String params,
            HttpServletRequest request) {
        JSONObject obj = JSONObject.parseObject(params);
        if (null != obj) {
            Map<String, Object> paramMap = MapTools.simpleMap(Common.PARAMS, params);
            RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/sync", BoCloudMethod.OTHERS,
                    null, paramMap, request);
            return service.invoke();
        } else {
            return ResultTools.formatErrResult();
        }
    }
}
